package com.burger.counter.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.burger.counter.Constants.SQLConstants;
import com.burger.counter.beans.Toppings;
import com.burger.counter.util.DBConnection;

public class ToppingDao {
	public Toppings[] getToppingsWithoutPriceDetails(int regionId){
		Connection con=null;
		ResultSet rs=null;
		PreparedStatement prest =null;
		Toppings toppingList[]=null;
		int size;
		try{
			con=DBConnection.getConnection();
			System.out.println("Creating DB Connection");
			String prepStatement=SQLConstants.toppingListWithoutPrice;
			prest = con.prepareStatement(prepStatement);
			prest.setInt(1,regionId);
			
			rs = prest.executeQuery();
			if (rs != null) {
				rs.last();
				size = rs.getRow();
				toppingList = new Toppings[size];
				int currRow = 0;
				rs.beforeFirst();

				while (rs.next()) {
					Toppings p = new Toppings();
					p.setToppingId(rs.getInt(1));
					p.setToppingName(rs.getString(2));
					toppingList[currRow] = p;
					currRow++;
				}
			}		

		}
		catch(Exception e){
			
		}
		
		return toppingList;
	}

	
	public Toppings[] getToppingsWithPriceDetails(int regionId){
		Connection con=null;
		ResultSet rs=null;
		PreparedStatement prest =null;
		Toppings toppingList[]=null;
		int size;
		try{
			con=DBConnection.getConnection();
			System.out.println("Creating DB Connection");
			String prepStatement=SQLConstants.toppingListWithPrice;
			prest = con.prepareStatement(prepStatement);
			prest.setInt(1,regionId);
			
			rs = prest.executeQuery();
			if (rs != null) {
				rs.last();
				size = rs.getRow();
				toppingList = new Toppings[size];
				int currRow = 0;
				rs.beforeFirst();

				while (rs.next()) {
					Toppings p = new Toppings();
					p.setToppingId(rs.getInt(1));
					p.setToppingName(rs.getString(2));
					p.setToppingExtraPrice(rs.getDouble(3));
					toppingList[currRow] = p;
					currRow++;
				}
			}		

		}
		catch(Exception e){
			
		}
		
		return toppingList;
	}
	
	public void setToppingCount(int regionId,String toppingName){
		Connection con=null;
		int rs;
		PreparedStatement prest =null;
		try{
			con=DBConnection.getConnection();
			System.out.println("Creating DB Connection");
			String prepStatement=SQLConstants.updateToppingCount;
			prest = con.prepareStatement(prepStatement);
			prest.setInt(1, regionId);
			prest.setString(2, toppingName);
			
			rs = prest.executeUpdate();
			if (rs == 1) {
				System.out.println("Topping Count has been updated");
			}					

		}
		catch(Exception e){
			
		}		
	}

}
